//
//  MPDirectoryHelper.m
//  Meng800
//
//  Created by 姚宗超 on 14/7/19.
//  Copyright (c) 2014年 点财网. All rights reserved.
//

#import "MPDirectoryHelper.h"

@implementation MPDirectoryHelper

//////////////////////////
// 文件存放目录
static const NSString* kDCWLogFileDir = @"log";
static const NSString* kDCWChatFileDir = @"chat";
static const NSString* kMPPersistDir = @"persist";
static const NSString* KMPDBDir = @"DB";

static const NSString* kMPPostImageCacheDir = @"post/image";

static const NSString* kMPPostImageThumbnailCacheDir = @"post/thumbnail_image";
static const NSString* kMPGetImageThumbnailCacheDir = @"get/thumbnail_image";

static const NSString* kMPPostAudioCacheDir = @"post/audio_cache";
static const NSString* kMPGetAudioCacheDir = @"get/audio_cache";

+ (BOOL)createDirIfNeccesary:(NSString *)dirPath
{
    BOOL isDir = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:dirPath isDirectory:&isDir];
    bool isCreated = false;
    if ( !(isDir == YES && existed == YES) )
    {
        isCreated = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return isCreated || existed;
}

// app所在的总的bundle文件夹
+ (NSString*)bundleDir
{
    //get the main bundel
    NSBundle *bundle = [NSBundle mainBundle];
    //get the path of home directory
    NSString *bundlePath = [bundle bundlePath];
    
    return bundlePath;
}

// document文件夹
+ (NSString*)documentDir
{
    /*use following c function to find any directory, just you need to provide the directory type*/
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    //find the actual path
    NSString* docDir = [paths objectAtIndex:0];
    return docDir;
}

// Library文件夹
+ (NSString*)libraryDir
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libDirectory = [paths objectAtIndex:0];
    return libDirectory;
}

// Library/caches
+ (NSString*)cachesDir
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDirectory = [paths objectAtIndex:0];
    return cachesDirectory;
}

// Library/applicationSupport itune会保存此文件夹
+ (NSString*)appSupportDir
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    NSString *dir = [paths objectAtIndex:0];
    return dir;
}

// tmp文件夹
+ (NSString*)temporaryDir
{
    return NSTemporaryDirectory();
}


// 业务相关文件夹
// Log文件夹
+ (NSString*)LogDir
{
    NSString* userId = @"";
    NSString* path = [NSString stringWithFormat:@"%@/%@", userId, kDCWLogFileDir];
    NSString* dirPath = [MPDocumentDir stringByAppendingPathComponent:path];
    BOOL succeed = [MPDirectoryHelper createDirIfNeccesary:dirPath];
    if (succeed) {
        return dirPath;
    }
    return nil;
}

// 聊天文件夹
+ (NSString*)chatFileDir
{
    NSString* userId = @"";
    NSString* path = [NSString stringWithFormat:@"%@/%@", userId, kDCWChatFileDir];
    NSString* dirPath = [MPDocumentDir stringByAppendingPathComponent:path];
    BOOL succeed = [MPDirectoryHelper createDirIfNeccesary:dirPath];
    if (succeed) {
        return dirPath;
    }
    return nil;
}

// 数据库文件夹
+ (NSString*)DBDir
{
    NSString* userId = @"";
    NSString* path = [NSString stringWithFormat:@"%@/%@", userId, KMPDBDir];
    NSString* dirPath = [MPDocumentDir stringByAppendingPathComponent:path];
    BOOL succeed = [MPDirectoryHelper createDirIfNeccesary:dirPath];
    if (succeed) {
        return dirPath;
    }
    return nil;
}

// 数据存储文件夹
+ (NSString*)persistDir
{
    NSString* userId = @"";
    NSString* path = [NSString stringWithFormat:@"%@/%@", userId, kMPPersistDir];
    NSString* dirPath = [MPDocumentDir stringByAppendingPathComponent:path];
    BOOL succeed = [MPDirectoryHelper createDirIfNeccesary:dirPath];
    if (succeed) {
        return dirPath;
    }
    return nil;
}

// 发送图片缓存文件夹
+ (NSString*)postImageCacheDir
{
    NSString* userId = @"";
    NSString* path = [NSString stringWithFormat:@"%@/%@", userId, kMPPostImageCacheDir];
    NSString* dirPath = [MPDocumentDir stringByAppendingPathComponent:path];
    BOOL succeed = [MPDirectoryHelper createDirIfNeccesary:dirPath];
    if (succeed) {
        return dirPath;
    }
    return nil;
}

// 发送图片缩略图缓存文件夹
+ (NSString*)postImageThumbnailCacheDir
{
    NSString* userId = @"";
    NSString* path = [NSString stringWithFormat:@"%@/%@", userId, kMPPostImageThumbnailCacheDir];
    NSString* dirPath = [MPDocumentDir stringByAppendingPathComponent:path];
    BOOL succeed = [MPDirectoryHelper createDirIfNeccesary:dirPath];
    if (succeed) {
        return dirPath;
    }
    return nil;
}

// 收到图片缩略图缓存文件夹
+ (NSString*)getImageThumbnailCacheDir
{
    NSString* userId = @"";
    NSString* path = [NSString stringWithFormat:@"%@/%@", userId, kMPGetImageThumbnailCacheDir];
    NSString* dirPath = [MPDocumentDir stringByAppendingPathComponent:path];
    BOOL succeed = [MPDirectoryHelper createDirIfNeccesary:dirPath];
    if (succeed) {
        return dirPath;
    }
    return nil;
}


// 发送音频文件夹
+ (NSString*)postAudioCacheDir
{
    NSString* userId = @"";
    NSString* path = [NSString stringWithFormat:@"%@/%@", userId, kMPPostAudioCacheDir];
    NSString* dirPath = [MPDocumentDir stringByAppendingPathComponent:path];
    BOOL succeed = [MPDirectoryHelper createDirIfNeccesary:dirPath];
    if (succeed) {
        return dirPath;
    }
    return nil;
}

// 音频缓存文件夹
+ (NSString*)getAudioCacheDir
{
    NSString* userId = @"";
    NSString* path = [NSString stringWithFormat:@"%@/%@", userId, kMPGetAudioCacheDir];
    NSString* dirPath = [MPDocumentDir stringByAppendingPathComponent:path];
    BOOL succeed = [MPDirectoryHelper createDirIfNeccesary:dirPath];
    if (succeed) {
        return dirPath;
    }
    return nil;
}

@end
